<template>
  <el-form ref="form" :model="form" label-width="80px">
    <el-form-item label="学生姓名">
      <el-select v-model="form.userId" filterable placeholder="请选择" clearable style="width: 100%;">
        <el-option
          v-for="item in studentData"
          :key="item.user_Id"
          :label="item.userName+'-'+item.nickName"
          :value="item.user_Id"
        >
          <i class="el-icon-user-solid">{{item.userName+'-'+item.nickName}}</i>
        </el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="企业名称">
      <el-input
        v-model="form.enterPrise"
        autocomplete="off"
        prefix-icon="el-icon-office-building"
        maxlength="32"
        show-word-limit
        resize
        clearable
      ></el-input>
    </el-form-item>
    <el-form-item label="当前状态">
      <el-select
        icon="el-icon-s-flag"
        v-model="form.status"
        placeholder="请选择状态"
        resize
        class="setDialogDateAndSex"
        clearable
        style="width: 100%;"
      >
        <el-option label="在职" value="在职">
          <i class="el-icon-success">在职</i>
        </el-option>
        <el-option label="离职" value="离职">
          <i class="el-icon-error">离职</i>
        </el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="入职时间">
      <!-- <el-col :span="11"> -->
      <el-date-picker
        v-model="form.entryTime"
        type="datetime"
        placeholder="选择入职时间"
        default-time="12:00:00"
        style="width:100%;"
      ></el-date-picker>
      <!-- </el-col> -->
    </el-form-item>
    <el-form-item label="离职时间">
      <!-- <el-col :span="11"> -->
      <el-date-picker
        v-model="form.dimissionTime"
        type="datetime"
        placeholder="选择离职时间"
        default-time="12:00:00"
        style="width:100%;"
      ></el-date-picker>
      <!-- </el-col> -->
    </el-form-item>
    <el-form-item label="离职原因">
      <el-input type="textarea" v-model="form.dimissionCause"></el-input>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="handleAdd">立即登记</el-button>
      <el-button @click="goBack">取消</el-button>
    </el-form-item>
  </el-form>
</template>
<script>
export default {
  data () {
    return {
      studentData: [],
      form: {
        userId: '',
        status: '', // 入职状态
        enterPrise: '', // 企业名称
        entryTime: '', // 入职时间
        dimissionTime: '', // 离职时间
        dimissionCause: '' // 离职原因
      }
    }
  },
  created () {
    this.getStudentData()
  },
  methods: {
    goBack () {
      // 点击跳转至上次浏览页面
      if (this.$store.state.user.roleName === '管理员') {
        this.$router.replace('/employmentManagement')
      } else {
        this.$router.replace('/index')
      }
    },
    handleAdd: function () {
      // 执行添加请假信息方法
      //   alert(JSON.stringify(this.form))
      if (this.form.dimissionTime === '') {
        this.form.dimissionTime = null
      } else if (this.form.entryTime === '') {
        this.form.entryTime = null
      }
      if (this.form != null) {
        this.$axios
          .post('/employments', {
            json: this.form
          })
          .then(response => {
            if (response.data.code === 200) {
              // alert(JSON.stringify(response.data));
              setTimeout(() => {
                // var obj = JSON.parse(JSON.stringify(response.data.data))
                this.$message({
                  message: '添加成功',
                  type: 'success'
                })
                this.getStudentData()
                this.initInsert()
              }, 300)
            } else {
              this.$message({
                message: '添加失败',
                type: 'warning'
              })
            }
          })
          .catch(function (error) {
            // 请求失败处理
            console.log(error)
          })
      }
    },
    initInsert () {
      this.form.userId = ''
      this.form.status = ''
      this.form.enterPrise = ''
      this.form.entryTime = ''
      this.form.dimissionTime = ''
      this.form.dimissionCause = ''
    },
    getStudentData () {
      // 获取学生信息
      this.$axios
        .get('/leaves/studentInfos')
        .then(response => (this.studentData = response.data.data))
        .catch(function (error) {
          // 请求失败处理
          console.log(error)
        })
    }
  }
}
</script>
